package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.DeviceTypeMaintainMapper;
import com.indusfo.spc.mapper.MaintainMapper;
import com.indusfo.spc.mapper.MaintainPlanDetailMapper;
import com.indusfo.spc.mapper.MaintainWorkDetailMapper;
import com.indusfo.spc.pojo.Maintain;
import com.indusfo.spc.service.MaintainService;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MaintainServiceImpl implements MaintainService {

    private  static  final Logger logger = LoggerFactory.getLogger(MaintainServiceImpl.class);

    @Resource
    private MaintainMapper maintainMapper;

    @Resource
    private MaintainWorkDetailMapper maintainWorkDetailMapper;

    @Resource
    private MaintainPlanDetailMapper maintainPlanDetailMapper;

    @Resource
    private DeviceTypeMaintainMapper deviceTypeMaintainMapper;

    /**
     * 编辑
     *
     * @param maintain
     * @return  JSONObject
     */
    @Override
    public JSONObject updateMaintain(Maintain maintain) {
        try {
            if(maintain.getMaintainId() == null) {
                throw new ParamsErrorException("请选择要编辑的保养项目");
            }
            checkParam(maintain);
            // 调用存储过程
            int row = maintainMapper.updateByPrimaryKeySelective(maintain);
            if(row == 0) {
                throw new ModifyFailedException("数据编辑失败");
            }
            return JSONObject.oK("编辑成功");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /***
     * 查询（分页）
     *
     * @return com.indusfo.spc.vo.JSONObject
     */
    @Override
    public JSONObject selectAll(Maintain maintain) {
        List<Maintain> listMaintain = null;
        try {
            Integer pagesize = maintain.getPagesize();
            Integer pageindex = maintain.getPageindex();
            if(pagesize != null && pageindex != null) {
                maintain.setIncept(pagesize*(pageindex - 1));
            }
            //查询全部
            listMaintain = maintainMapper.selectAll(maintain);
            if (listMaintain.isEmpty()) {
                return JSONObject.oK("没有查询到数据", listMaintain, 0);
            }
            //查询分页总记录数
            int count = maintainMapper.countMaintain(maintain); //返回查询到的总记录数
            return JSONObject.oK("查询成功", listMaintain, count);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /***
     * 新增
     *
     * @param maintain
     * @return com.indusfo.spc.vo.JSONObject
     */
    @Override
    public JSONObject insertMaintain(Maintain maintain) {
        try {
            if(maintain.getMaintainId() != null) {
                throw new ParamsErrorException("新增时，不能填写id");
            }
            checkParam(maintain);
            int	row = maintainMapper.insertSelective(maintain);
            if(row == 0) {
                throw new ModifyFailedException("数据新增失败");
            }
            return JSONObject.oK("新增成功");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 1 启用  2 删除  3停用 不良对策
     *
     * @param maintainIds
     * @param dataState
     * @return com.indusfo.spc.vo.JSONObject
     */
    @Override
    public JSONObject definedMaintain(Long[] maintainIds, Integer dataState) {
        try {
            if(dataState == null) {
                throw new ParamsErrorException("数据状态不能为空");
            }else if(dataState != 1 && dataState != 2 && dataState != 3) {
                throw new ParamsErrorException("数据状态错误");
            }
            if (maintainIds == null || maintainIds.length == 0) {
                throw new ParamsErrorException("保养项目ID不能为空");
            }
            if(dataState == 2){
                int j = deviceTypeMaintainMapper.selectByMaintainId(maintainIds);
                if(j>0){
                    throw new ParamsErrorException("保养项目正在被设备类型使用");
                }
                int k = maintainPlanDetailMapper.selectByMaintainId(maintainIds);
                if(k>0){
                    throw new ParamsErrorException("保养项目正在被保养计划使用");
                }
                int i = maintainWorkDetailMapper.selectByMaintainId(maintainIds);
                if(i>0){
                    throw new ParamsErrorException("保养项目正在被保养作业使用");
                }
            }
            // 执行存储过程
            int row = maintainMapper.definedMaintain(maintainIds, dataState);
            String msg = "";
            if (row == 0) {
                // 判断传入的数据状态参数lDataState,返回相应信息
                switch (dataState) {
                    case 1:
                        throw new ModifyFailedException("启用失败");
                    case 2:
                        throw new ModifyFailedException("删除失败");
                    case 3:
                        throw new ModifyFailedException("停用失败");
                    default:
                }
            } else {
                switch (dataState) {
                    case 1:
                        msg = "启用成功";break;
                    case 2:
                        msg = "删除成功";break;
                    case 3:
                        msg = "停用成功";break;
                    default:
                }
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 保养项目校验
     * @param maintain
     */
    public void checkParam(Maintain maintain){
        if(StringUtils.isEmpty(maintain.getMaintainCode())){
            throw new ParamsErrorException("保养项目编码不能为空");
        }
        if (maintain.getMaintainCode().length() > 50) {
            throw new ParamsErrorException("保养项目编码字数超过规定,长度50");
        }
        if(StringUtils.isEmpty( maintain.getMaintainName())){
            throw new ParamsErrorException("保养项目名称不能为空");
        }
        if (maintain.getMaintainName().length() > 50) {
            throw new ParamsErrorException("保养项目名称字数超过规定,长度50");
        }
        // 编码不能重复
        Integer CodeCounts = maintainMapper.queryMaintainCode(maintain);
        if (CodeCounts !=0) {
            throw new ParamsErrorException("该保养项目编码已存在");
        }
        // 名称不能重复
        Integer NameCounts = maintainMapper.queryMaintainName(maintain);
        if (NameCounts !=0) {
            throw new ParamsErrorException("该保养项目名称已存在");
        }
    }
    
}
